package com.api.phm.util;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommandUtils {
    /**
     * @param cmdOrder    命令
     * @param charsetName 字符类型
     * @param exceptNum   忽略行数
     * @return
     */
    public static Map<String, Object> runCmdByWindows(String cmdOrder, String charsetName, int exceptNum) {
        charsetName = StringUtils.isBlank(charsetName) ? "GBK" : charsetName;
        Map<String, Object> map = new HashMap<>();
        List<String> list = new ArrayList<>();
        Boolean flag = true;
        try {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec(cmdOrder);

            // 读取命令执行结果
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), charsetName));
            String line;
            int i = 0;
            while ((line = reader.readLine()) != null) {
                if (i > exceptNum || exceptNum == 0) {
                    list.add(line);
                }
                i++;
//                System.out.println(line);
            }
            System.out.println(list.size());
            // 等待命令执行完成
            process.waitFor();

            // 获取退出值
            int exitValue = process.exitValue();
//            System.out.println("Exit value: " + exitValue);
        } catch (Exception e) {
            flag = false;
            System.out.println(e.getMessage());
        } finally {
            map.put("data", list);
            map.put("flag", flag);
            return map;
        }
    }

    ;

    public static void main(String[] args) {
        String cmdOrder = "ping www.baidu.com";
//        String cmdOrder2 = "net use \\\\172.23.24.192\\dat /user:admin hscn2022";
//        Boolean flag2 = runCmdByWindows(cmdOrder2,"GBK");
//        System.out.println("ComRun Status : " + flag2);
//        cmdOrder = "D:\\Project\\AGC\\bin\\read_dat.exe 1 120 0 20240517165500 60 \\\\10.97.236.43\\dat\\m1_d1\\ RMS 20";
//        List<Map<String, Object>> rList = new ArrayList<>();
//        cmdOrder = "D:\\Project\\getDatData.bat 10.97.236.43 hscn hscn2023 m1_d1\\ 1 20240517165500";
//        Map<String, Object> map = runCmdByWindows(cmdOrder, "GBK", 5);
//        System.out.println("ComRun Status : " + map.get("flag"));
//        System.out.println("ComRun list : " + map.get("data"));
//        List<String> list = JSONArray.parseArray(map.get("data").toString(), String.class);
////        Calendar calendar = new Calendar(new Date());
//        Date date = new Date();
//        System.out.println(date);
//        System.out.println(date.getTime());
//        System.out.println(new Date(date.getTime()));
//        Long timeLong = date.getTime();
//        String dattime = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHHMMSS, date) + "00";
//        System.out.println(dattime);
//        List<Map> timeList = new ArrayList<>();
//        for (int i = 0; i < list.size(); i++) {
//            Map<Long, Double> timeMap = new HashMap<>();
//            Long key = timeLong + (i * 20);
//            Double s = Double.valueOf(list.get(i));
//            timeMap.put(key, s);
//            timeList.add(timeMap);
//        }
//        System.out.println(timeList.toString());

        runCmdByWindows("ping 10.97.236.43", "gbk", 0);
    }

}
